package com.gzx.plugin.tjzy.modular.phone.complaint.service.impl;

import com.baomidou.dynamic.datasource.annotation.DS;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.gzx.plugin.tjzy.modular.phone.complaint.bo.GzxComplaintPhoneAddOrEditBo;
import com.gzx.plugin.tjzy.modular.phone.complaint.bo.GzxComplaintPhoneQueryBo;
import com.gzx.plugin.tjzy.modular.phone.complaint.entity.GzxComplaintPhone;
import com.gzx.plugin.tjzy.modular.phone.complaint.mapper.GzxComplaintPhoneMapper;
import com.gzx.plugin.tjzy.modular.phone.complaint.service.GzxComplaintPhoneDbService;
import com.gzx.plugin.tjzy.modular.phone.complaint.vo.GzxComplaintPhoneVo;
import com.gzx.plugin.tjzy.modular.phone.dto.GzxPhoneStatusDto;
import org.springframework.stereotype.Service;
import vip.xiaonuo.common.mvc.service.impl.BaseServiceImpl;
import vip.xiaonuo.common.util.CommonCryptogramUtil;
import vip.xiaonuo.common.util.CommonDateUtil;
import vip.xiaonuo.common.util.CommonSleepUtil;

import java.util.List;
import java.util.stream.Collectors;

import static org.apache.commons.lang.StringEscapeUtils.escapeSql;

@Service
@DS("tjzy-async")
public class GzxComplaintPhoneDbServiceImpl extends BaseServiceImpl<GzxComplaintPhoneAddOrEditBo, GzxComplaintPhoneVo, GzxComplaintPhoneQueryBo, GzxComplaintPhoneMapper, GzxComplaintPhone> implements GzxComplaintPhoneDbService {
    public GzxComplaintPhoneDbServiceImpl(GzxComplaintPhoneMapper baseMapper) {
        super(baseMapper);
    }

    @Override
    @DS("tjzy-async")
    public List<GzxComplaintPhone> selectList(LambdaQueryWrapper<GzxComplaintPhone> lambdaQueryWrapper) {
        return baseMapper.selectList(lambdaQueryWrapper);
    }

    @Override
    @DS("tjzy-async")
    public void saveBatch(List<GzxPhoneStatusDto> phoneStatusDtoList, String userId, String tenantId) {
        String todayStr = CommonDateUtil.getCurrentMonth();
        String currentTime = CommonDateUtil.getCurrentTimeStr();
        String batchSql = phoneStatusDtoList.stream()
                .map(s -> String.format("('%s', %s,  %s, %s, '%s','%s', '%s', '%s')",
                        escapeSql(CommonCryptogramUtil.doSm1AesEncrypt(s.getPhone())),
                        escapeSql(String.valueOf(s.getType())),
                        escapeSql("1"),
                        escapeSql(todayStr),
                        escapeSql(s.getAllJson()),
                        escapeSql(currentTime),
                        escapeSql(tenantId),
                        escapeSql(userId)))
                .collect(Collectors.joining(", ")).replace("'null'", "null");
        baseMapper.insertBatchSql(batchSql);
        CommonSleepUtil.sleep100();
    }

    @Override
    protected LambdaQueryWrapper<GzxComplaintPhone> buildQueryWrapper(GzxComplaintPhoneQueryBo queryBo) {
        return Wrappers.<GzxComplaintPhone>lambdaQuery().orderByDesc(GzxComplaintPhone::getId);
    }
}
